/**
 * 
 */
package com.cnhaimei.vod.net.dq;

import java.io.ByteArrayInputStream;
import java.util.Arrays;
import java.util.HashMap;

import android.util.Log;

import com.cnhaimei.vod.net.Response;
import com.cnhaimei.vod.util.Util;

/**
 * @author jin001.wu@gmail.com <br>
 * @date 2011-7-17
 * 
 */
public class DQQueryResult extends Response {

	public static final String TAG = "Records";

	// private String queryTable;
	// private String queryString;

	private HashMap<String, Field> fields = new HashMap<String, Field>();

	private int recordLimit; // 单次传输记录限度;
	private int recordTotal; // 查询结果记录总数;
	private int recordBytes; // 记录长度;
	private int fieldsCount; // 字段个数;
	private byte snap; // 快照 0,为查询失败
	private long id;

	public Field getField(String name) {
		return fields.get(name);
	}

	public int getRecordLimit() {
		return recordLimit;
	}

	public int getRecordTotal() {
		return recordTotal;
	}

	public int getRecordBytes() {
		return recordBytes;
	}

	public int getFieldsCount() {
		return fieldsCount;
	}

	public byte getSnap() {
		return snap;
	}

	public void setSnap(byte snap) {
		this.snap = snap;
	}

	public static class Field {
		private static byte[] NAME = new byte[32];
		public String name;
		public int type;
		public int size;
		public int posi;
		public int sort;

		public static Field parse(ByteArrayInputStream in) {
			Field f = new Field();
			Arrays.fill(NAME, DQManager.Zero);
			in.read(NAME, 0, NAME.length);

			f.name = DQManager.readString(NAME);
			f.type = Util.readInt(in);// (byte) (in.read() & 0xff);
			f.size = Util.readInt(in);
			f.posi = Util.readInt(in);
			f.sort = Util.readInt(in);

			Log.e(TAG, "name:" + f.name);
			Log.e(TAG, "type" + f.type);
			Log.e(TAG, "size:" + f.size);
			Log.e(TAG, "posi" + f.posi);
			Log.e(TAG, "sor" + f.sort);

			return f;
		}
	}
	
	public  boolean parse(ByteArrayInputStream in, int length) {
		recordLimit = Util.readInt(in); // 单次传输记录限度;
		recordTotal = Util.readInt(in); // 查询结果记录总数;
		recordBytes = Util.readInt(in); // 记录长度;
		fieldsCount = Util.readInt(in); // 字段个数;

		Log.e(TAG, "rtn.recordsLimit:" + recordLimit);
		Log.e(TAG, "rtn.recordTotal:" + recordTotal);
		Log.e(TAG, "rtn.recordBytes:" + recordBytes);
		Log.e(TAG, "rtn.fieldsCount:" + fieldsCount);

		for (int i = 0; i < fieldsCount; i++) {
			Field f = Field.parse(in);
			if (f != null) {
				fields.put(f.name, f);
			}
		}

		return true;
	}

//	public static DQQueryResponse parse(ByteArrayInputStream in, int length) {
//		DQQueryResponse rtn = new DQQueryResponse();
//		rtn.recordLimit = Util.readInt(in); // 单次传输记录限度;
//		rtn.recordTotal = Util.readInt(in); // 查询结果记录总数;
//		rtn.recordBytes = Util.readInt(in); // 记录长度;
//		rtn.fieldsCount = Util.readInt(in); // 字段个数;
//
//		Log.e(TAG, "rtn.recordsLimit:" + rtn.recordLimit);
//		Log.e(TAG, "rtn.recordTotal:" + rtn.recordTotal);
//		Log.e(TAG, "rtn.recordBytes:" + rtn.recordBytes);
//		Log.e(TAG, "rtn.fieldsCount:" + rtn.fieldsCount);
//
//		for (int i = 0; i < rtn.fieldsCount; i++) {
//			Field f = Field.parse(in);
//			if (f != null) {
//				rtn.fields.put(f.name, f);
//			}
//		}
//
//		return rtn;
//	}

	/**
	 * @return the id
	 */
	public long getId() {
		return id;
	}

	/**
	 * @param id
	 *            the id to set
	 */
	public void setId(long id) {
		this.id = id;
	}

}
